package com.adorkable.iosdialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


public class AlertDialog {
    private Context context;
    private Dialog dialog;
    private LinearLayout container;
    private TextView titleTv;
    private TextView msgTv;
    private Button negBtn;
    private Button posBtn;
    private View img_line;
    private Display display;
    private boolean showTitle = false;
    private boolean showMsg = false;
    private boolean showPosBtn = false;
    private boolean showNegBtn = false;

    public AlertDialog(Context context) {
        this.context = context;
        WindowManager windowManager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        display = windowManager.getDefaultDisplay();
    }

    public AlertDialog init() {
        // 获取Dialog布局
        View view = LayoutInflater.from(context).inflate(R.layout.view_alertdialog, null);

        // 获取自定义Dialog布局中的控件
        container = view.findViewById(R.id.container);
        titleTv = view.findViewById(R.id.txt_title);
        titleTv.setVisibility(View.GONE);
        msgTv = view.findViewById(R.id.txt_msg);
        msgTv.setVisibility(View.GONE);
        negBtn = view.findViewById(R.id.btn_neg);
        negBtn.setVisibility(View.GONE);
        posBtn = view.findViewById(R.id.btn_pos);
        posBtn.setVisibility(View.GONE);
        img_line = view.findViewById(R.id.img_line);
        img_line.setVisibility(View.GONE);

        // 定义Dialog布局和参数
        dialog = new Dialog(context, R.style.AlertDialogStyle);
        dialog.setContentView(view);

        // 调整dialog背景大小
        container.setLayoutParams(
                new FrameLayout.LayoutParams(
                        (int) (display.getWidth() * 0.85),
                        LinearLayout.LayoutParams.WRAP_CONTENT));

        return this;
    }


    public AlertDialog setTitle(String title, int textSize, int textColor) {
        showTitle = true;
        if (titleTv == null) return this;

        String realTitle = (title == null || title.equals("")) ? "标题" : title;
        titleTv.setText(realTitle);

        int realSize = textSize == 0 ? 14 : textSize;
        titleTv.setTextSize(realSize);

        try {
            titleTv.setTextColor(textColor);
        }catch (Exception e){
            titleTv.setTextColor(Color.parseColor("0xFF000000"));
        }
        return this;
    }

    public AlertDialog setMessage(String message, int textSize, int textColor){
        showMsg = true;
        if (msgTv == null) return this;

        String realTitle = (message == null || message.equals("")) ? "内容" : message;
        msgTv.setText(realTitle);

        int realSize = textSize == 0 ? 14 : textSize;
        msgTv.setTextSize(realSize);

        try {
            msgTv.setTextColor(textColor);
        }catch (Exception e){
            msgTv.setTextColor(Color.parseColor("0xFF000000"));
        }
        return this;
    }

    public AlertDialog setCancelable(boolean cancel) {
        dialog.setCancelable(cancel);
        return this;
    }



    public AlertDialog setPositiveButton(String text, int textSize, int textColor, int bgColor,
                                         final View.OnClickListener listener) {
        showPosBtn = true;

        if (posBtn == null) return this;

        String realTitle = (text == null || text.equals("")) ? "确认" : text;
        posBtn.setText(realTitle);

        int realSize = textSize == 0 ? 14 : textSize;
        posBtn.setTextSize(realSize);

        try {
            posBtn.setTextColor(textColor);
        }catch (Exception e){
            posBtn.setTextColor(Color.parseColor("0xFF000000"));
        }

        try {
            posBtn.setBackgroundColor(bgColor);
        }catch (Exception e){
            posBtn.setBackground(context.getDrawable(R.drawable.alertdialog_right_selector));
        }

        posBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onClick(v);
                }
                dialog.dismiss();
            }
        });
        return this;
    }

    public AlertDialog setNegativeButton(String text, int textSize, int textColor, int bgColor,
                                         final View.OnClickListener listener) {
        showNegBtn = true;


        if (negBtn == null) return this;

        String realTitle = (text == null || text.equals("")) ? "确认" : text;
        negBtn.setText(realTitle);

        int realSize = textSize == 0 ? 14 : textSize;
        negBtn.setTextSize(realSize);

        try {
            negBtn.setTextColor(textColor);
        }catch (Exception e){
            negBtn.setTextColor(Color.parseColor("0xFF000000"));
        }

        try {
            negBtn.setBackground(context.getDrawable(bgColor));
        }catch (Exception e){
            negBtn.setBackground(context.getDrawable(R.drawable.alertdialog_right_selector));
        }

        negBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onClick(v);
                }
                dialog.dismiss();
            }
        });
        return this;
    }

//    public AlertDialog setNegativeButton(String text, int textColor, final View.OnClickListener listener) {
//        if (negBtn != null){
//            negBtn.setTextColor(textColor);
//        }
//        showNegBtn = true;
//        if ("".equals(text)) {
//            negBtn.setText("取消");
//        } else {
//            negBtn.setText(text);
//        }
//        negBtn.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (listener != null) {
//                    listener.onClick(v);
//                }
//                dialog.dismiss();
//            }
//        });
//        return this;
//    }


    private void setLayout() {
        if (!showTitle && !showMsg) {
            titleTv.setText("提示");
            titleTv.setVisibility(View.VISIBLE);
        }

        if (showTitle) {
            titleTv.setVisibility(View.VISIBLE);
        }

        if (showMsg) {
            msgTv.setVisibility(View.VISIBLE);
        }

        if (!showPosBtn && !showNegBtn) {
            posBtn.setText("确定");
            posBtn.setVisibility(View.VISIBLE);
            posBtn.setBackgroundResource(R.drawable.alertdialog_single_selector);
            posBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
        }

        if (showPosBtn && showNegBtn) {
            posBtn.setVisibility(View.VISIBLE);
            posBtn.setBackgroundResource(R.drawable.alertdialog_right_selector);
            negBtn.setVisibility(View.VISIBLE);
            negBtn.setBackgroundResource(R.drawable.alertdialog_left_selector);
            img_line.setVisibility(View.VISIBLE);
        }

        if (showPosBtn && !showNegBtn) {
            posBtn.setVisibility(View.VISIBLE);
            posBtn.setBackgroundResource(R.drawable.alertdialog_single_selector);
        }

        if (!showPosBtn && showNegBtn) {
            negBtn.setVisibility(View.VISIBLE);
            negBtn.setBackgroundResource(R.drawable.alertdialog_single_selector);
        }
    }

    public void show() {
        setLayout();
        dialog.show();
    }
}
